package com.temp.controller.admin;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.temp.dto.SearchCampaignDTO;
import com.temp.dto.SetCampaignDTO;
import com.temp.entity.Campaign;
import com.temp.enumeration.CampaignStatus;
import com.temp.result.Result;
import com.temp.service.CampaignService;
import com.temp.vo.CampaignDetailVO;
import com.temp.vo.CustomPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api/admin/campaign")
@Api(tags = "管理活动相关接口")
@Slf4j
@Validated
public class AdminCampaignController {

    @Autowired
    private CampaignService campaignService;

    @PostMapping("/query")
    @ApiOperation("管理员获取全部活动")
    @RequiresPermissions("admin:campaign:query")
    public Result getAllCampaign(@RequestBody SearchCampaignDTO campaignDTO) {
        campaignDTO.formatParam();
        CustomPageVO<CampaignDetailVO> adminCampaignData = campaignService.findAdminCampaignByStatus(campaignDTO);
        return Result.success(adminCampaignData,"apiMsg.querySuccess");
    }

    @GetMapping("/status")
    @ApiOperation("管理员更新活动状态")
    @RequiresPermissions("admin:campaign:status")
    public Result updateStatus(@RequestParam Long id, @RequestParam CampaignStatus status) {
        if (!status.equals(CampaignStatus.ACTIVE) && !status.equals(CampaignStatus.DRAFT)) {
            return Result.error("apiMsg.optionError");
        }
        Campaign campaign = campaignService.getOne(Wrappers.<Campaign>lambdaQuery().eq(Campaign::getId, id));
        LambdaUpdateWrapper<Campaign> updateWrapper = Wrappers.<Campaign>lambdaUpdate()
                .eq(Campaign::getId, id)
                .set(Campaign::getStatus, status.getValue());
        if (status.equals(CampaignStatus.ACTIVE) && campaign.getStartTime() == null) {
                updateWrapper.set(Campaign::getStartTime, LocalDateTime.now());
        }
        return campaignService.update(updateWrapper)
                ? Result.success("apiMsg.optionSuccess")
                : Result.error("apiMsg.optionError");
    }

    @PostMapping("/update")
    @ApiOperation("管理员活动的设置")
    @RequiresPermissions("admin:campaign:update")
    public Result updateCampaign(@Valid @RequestBody SetCampaignDTO campaignDTO) {
        boolean isSuccess = campaignService.updateCampaignSet(campaignDTO);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }

    @DeleteMapping("/delete")
    @ApiOperation("管理员删除活动")
    @RequiresPermissions("admin:campaign:delete")
    @Transactional(rollbackFor = Exception.class)
    public Result delCampaign(@RequestParam Long id) {
        boolean isSuccess = campaignService.removeById(id);

        return isSuccess ? Result.success("apiMsg.optionSuccess") : Result.error("apiMsg.optionError");
    }
}
